package com.ftg.learn.day15.Javabase;

import com.ftg.learn.day15.Javabase.mapper.DeptMapper;
import com.mysql.cj.jdbc.MysqlDataSource;
import org.apache.ibatis.binding.MapperProxy;
import org.apache.ibatis.binding.MapperRegistry;
import org.apache.ibatis.builder.xml.XMLConfigBuilder;
import org.apache.ibatis.builder.xml.XMLMapperBuilder;
import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.parsing.XNode;
import org.apache.ibatis.session.*;
import org.apache.ibatis.session.defaults.DefaultSqlSession;
import org.apache.ibatis.session.defaults.DefaultSqlSessionFactory;
import org.apache.ibatis.transaction.Transaction;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransaction;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;

/**
 * @author 布衣
 */
public class MybatisUtil1{

    public static void main(String[] args) {
        Configuration configuration = new Configuration();
        configuration.setLogImpl(StdOutImpl.class);

        String id = "localmysql";
        TransactionFactory transactionFactory = new JdbcTransactionFactory();
        DataSource dataSource = new PooledDataSource("com.mysql.cj.jdbc.Driver","jdbc:mysql://localhost:3306/db2402","root","123456");

        Environment env = new Environment(id,transactionFactory,dataSource);

        configuration.setEnvironment(env);


        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(configuration);

//        SqlSessionFactory sqlSessionFactory = new DefaultSqlSessionFactory(configuration);

        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.REUSE,TransactionIsolationLevel.SERIALIZABLE);

//        Transaction t = new JdbcTransaction();
//        Executor executor = configuration.newExecutor(t);
//        SqlSession sqlSession = new DefaultSqlSession(configuration,executor,false);
        sqlSession.getConfiguration().addMapper(DeptMapper.class);

        //注入xml
        Reader reader = null;
        try {
            reader = Resources.getResourceAsReader("mybatis3/dept1.xml");
            XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(reader,configuration,"",null);
            xmlMapperBuilder.parse();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

//        XMLMapperBuilder xb = new XMLMapperBuilder("",configuration,"",null);

        DeptMapper deptMapper = sqlSession.getMapper(DeptMapper.class);

        System.out.println(deptMapper.showall1());
        //        MapperRegistry mr = new MapperRegistry(configuration);
//        mr.addMapper(DeptMapper.class);
//        DeptMapper deptMapper = mr.getMapper(DeptMapper.class,sqlSession);

    }

}
